package com.model;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
/**
 * 
 * @author Lars Nielsen
 * @version 2012-07-01
 * Model class for a person 
 */
public class Person {
	
	private String name, biography, country;
	private Date birthday, deathDay; //YYYY-MM-DD 
	private int age; 
	private Gender gender;
	private File picture;
	
	public File getPicture() {
		return picture;
	}



	public void setPicture(File picture) {
		this.picture = picture;
	}
	/*
	 * The following propperties is Bidirectional
	 */
	private Person partner, father, mother;
	private ArrayList<Person> formerPartners = new ArrayList<Person>();
	private ArrayList<Person> children = new ArrayList<Person>();
	
	//Todo check in crud methods that name are not short than to letters
	public Person(String name, String biography, Date birthday, Person father,
			Person mother, Gender gender) {
		this.name = name;
		this.biography = biography;
		this.birthday = birthday;
		this.father = father;
		this.mother = mother;
		this.gender = gender;
		this.age = this.getAge();
	}
	
	
	
	public String getName() {
		return name;
	}


	
	public void setName(String name) {
		this.name = name;
	}



	public String getBiography() {
		return biography;
	}



	public void setBiography(String biography) {
		this.biography = biography;
	}



	public String getCountry() {
		return country;
	}



	public void setCountry(String country) {
		this.country = country;
	}



	public Date getBirthday() {
		return birthday;
	}



	public void setBirthday(Date birthday) {
		this.birthday = birthday;
		setAge();
	}



	public Date getDeathDay() {
		return deathDay;
	}



	public void setDeathDay(Date deathDay) {
		this.deathDay = deathDay;
	}



	public int getAge() {
		setAge();
		return age;
	}

	private void setAge(){
		
		Date _currentDate = new Date();
		
		long _age = _currentDate.getTime() - this.birthday.getTime();
		_age = _age / 1000; //From milliseconds to seconds
		_age = _age / 60; // From seconds to minutes
		_age = _age / 60; //From minutes to hours
		_age = _age / 24; //From hours to days 
		_age = _age / 365; // From days to years - not calculating leap year
		age = (int) _age; //Make the age human readabl 
	}



	public Gender getGender() {
		return gender;
	}



	public void setGender(Gender gender) {
		this.gender = gender;
	}



	public Person getPartner() {
		return partner;
	}



	public void setPartner(Person partner) {
		
		
		if(!partner.equals(getPartner())){
			if(getPartner() != null){
				addFormerPartner(getPartner());
				this.partner = partner;
				this.partner.setPartner(this);
				
			}else{
				this.partner = partner;
				this.partner.setPartner(this);
			}
		}
		
		
	}



	public Person getFather() {
		return father;
	}



	public void setFather(Person father) {
		this.father = father;
		father.addChild(this);
		
	}



	public Person getMother() {
		return mother;
	}



	public void setMother(Person mother) {
		this.mother = mother;
		mother.addChild(this);
	}



	public ArrayList<Person> getFormerPartners() {
		return formerPartners;
	}



	public void setFormerPartners(ArrayList<Person> formerPartners) {
		this.formerPartners = formerPartners;
	}



	public ArrayList<Person> getChildren() {
		return children;
	}



	public void setChildren(ArrayList<Person> children) {
		this.children = children;
	}
	
	public void addFormerPartner(Person partner){
		formerPartners.add(partner);
		if(partner.getPartner() != null){
			partner.setPartner(null);
		}
		
	}
	
	public void addChild(Person child){
		
		if(this.getGender() == Gender.FEMALE){
			if(child.getMother() == null){
				child.setMother(this);
				children.add(child);
			}else{
				if(!child.getMother().equals(this)){
										
					children.add(child);
					child.getMother().removeChild(child);
					child.setMother(this);
				}
			}
		}else{
			if(child.getFather() == null){
				child.setFather(this);
				children.add(child);
			}else{
				if(!child.getFather().equals(this)){
					children.add(child);
					child.getFather().removeChild(child);
					child.setFather(this);
				}
			}
		}
		
		//Old code
		//Check if this person is a mother or father
//		if((!child.getFather().equals(this) || child.getFather() == null)
//				|| (!child.getMother().equals(this) || child.getMother() == null)){
//			if(this.gender == Gender.MALE){
//				child.setFather(this);
//			}
//			else{
//				child.setMother(this);
//			}
//		}
//		if(!children.contains(child)){
//			addChild(child);
//		}
		
	}
	
	public void removeChild(Person child){
		children.remove(child);
		if(child.getMother().equals(this)){
			child.setMother(null);
		}else if(child.getFather().equals(this)){
			child.setFather(this);			
		}
	}
	//For test
	@Override
	public String toString(){
		return "Name: " + this.getName() + " Age: " + this.getAge();
	}
	
	

}
